package com.example.project.service.impl;

import com.example.project.dao.LotteryExtDao;
import com.example.project.model.ResultObject;
import com.example.project.model.bo.LotteryBo;
import com.example.project.model.config.BaseConfigEnum;
import com.example.project.model.jooq.tables.daos.LotteryDao;
import com.example.project.service.InputExtBusinessService;
import com.example.project.service.base.impl.BaseBusinessServiceImpl;
import com.example.project.util.AssertUtils;
import com.example.project.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Created by Administrator on 2017/12/10.
 */
@Service
public class InputExtBusinessServiceImpl extends BaseBusinessServiceImpl implements InputExtBusinessService {

    @Autowired
    LotteryExtDao lotteryExtDao;

    @Autowired
    LotteryDao lotteryDao;

    /**
     * 1,2,3,4,5,6
     * ARRAY['虾','鱼','鸡','蟹','芦','钱']
     * 录入开奖数字
     * @param firstNum 第一个数字
     * @param secondNum 第二个数字
     * @param thirdNum 第三个数字
     * @return  返回
     */
    @Override
    @Transactional
    public ResultObject inputBonusNum(long firstNum, long secondNum, long thirdNum) {
        ResultObject resultObject = new ResultObject();
        try {
            String [] arr= {"葫芦","金钱","鱼","鸡","蟹","虾"};
            String [] arrUrl= {"/img/yx_1.jpg","/img/yx_2.jpg","/img/yx_3.jpg","/img/yx_4.jpg","/img/yx_5.jpg","/img/yx_6.jpg"};
            //最新开奖时间
            long lotterTime =0;
            long issueNo = 1000;
            LotteryBo lotteryBo = lotteryExtDao.loadAllNewestLotteryPo();
            if(AssertUtils.isNotNull(lotteryBo)&& AssertUtils.isNotNull(lotteryBo.getLotteryTime())){
                lotterTime= lotteryBo.getLotteryTime();
                issueNo=lotteryBo.getIssueNo();
            }
            /**
             * 1.最新时间距离当前时间是否超过5分钟了  5*60*1000毫秒
             * 超过5分钟，这最新开奖时间设定为当前时间，
             * 否则最新开奖时间要设定为 数据库最新时间+5*60*1000毫秒
             */
            LotteryBo lottery = new LotteryBo();
            lottery.setLotteryId(UUIDUtils.getUUIDShort());
            lottery.setFirstNum(firstNum);
            lottery.setFirstText(arr[(int) firstNum-1]);
            lottery.setFirstUrl(arrUrl[(int) firstNum-1]);
            lottery.setSecondNum(secondNum);
            lottery.setSecondText(arr[(int) secondNum-1]);
            lottery.setSecondUrl(arrUrl[(int) secondNum-1]);
            lottery.setThirdNum(thirdNum);
            lottery.setThirdText(arr[(int) thirdNum-1]);
            lottery.setThirdUrl(arrUrl[(int) thirdNum-1]);
            lottery.setSumNum(firstNum+secondNum+thirdNum);

            lottery.setIssueNo(issueNo+1);
            lottery.setInputflag("INPUT");
            long a = (System.currentTimeMillis()-lotterTime)/1000;
            if(a>295){
                lottery.setLotteryTime(System.currentTimeMillis());
            }else{
                lottery.setLotteryTime(lotterTime+5*60*1000);
            }
            lotteryDao.insert(lottery);
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultObject.setIenum(BaseConfigEnum.ERROR);
        }
        return resultObject;
    }

    /**
     * 定时任务产生开奖数字
     * @return
     */
    @Override
    @Transactional
    public ResultObject quartzBonusNum() {
        try {
            String [] arr= {"芦","钱","鱼","鸡","蟹","虾"};
            String [] arrUrl= {"/img/yx_1.jpg","/img/yx_2.jpg","/img/yx_3.jpg","/img/yx_4.jpg","/img/yx_5.jpg","/img/yx_6.jpg"};
            /**
             * 1.产生1-6的随机数字
             * 2.判断最新距离现在的时间是否小于5*60*1000
             * 如果小于的话，不产生，否则产生
             *
             * 1.判断是否有录入的数据
             * 有录入的时候的话，先修改最小一条录入数的状态
             * 没有录入数据的话，产生新的数据
             *
             */
            //最新开奖时间
            long lotterTime =0;
            long issueNo = 1000;
            boolean flag = true;
            LotteryBo lotteryBo = lotteryExtDao.loadAllNewestLotteryPo();
            if(AssertUtils.isNotNull(lotteryBo)){
                //最新的记录是录入的 需要更新最早的录入记录
                if(lotteryBo.getInputflag().equals("INPUT")){
                    flag =false;
                    LotteryBo lastLotteryBo = lotteryExtDao.loadInputLastLotteryPo();
                    lastLotteryBo.setInputflag("BONUS");
                    lastLotteryBo.setLotteryTime(System.currentTimeMillis());
                    lotteryDao.update(lastLotteryBo);
                }
            }
            //需要插入
            if(flag){

                if(AssertUtils.isNotNull(lotteryBo)&& AssertUtils.isNotNull(lotteryBo.getLotteryTime())){
                    lotterTime= lotteryBo.getLotteryTime();
                    issueNo=lotteryBo.getIssueNo();
                }
                //当前时间减去最新时间小于 5分钟，说明已经有手动写入的数据了，不错操作
                long a = (System.currentTimeMillis()-lotterTime)/1000;
                if(a>295){
                    //插入记录
                    long firstNum = (long) (Math.floor(Math.random()*(6))+1);
                    long secondNum = (long) (Math.floor(Math.random()*(6))+1);
                    long thirdNum = (long) (Math.floor(Math.random()*(6))+1);
                    /**
                     * 1.最新时间距离当前时间是否超过5分钟了  5*60*1000毫秒
                     * 超过5分钟，这最新开奖时间设定为当前时间，
                     * 否则最新开奖时间要设定为 数据库最新时间+5*60*1000毫秒
                     */
                    LotteryBo lottery = new LotteryBo();
                    lottery.setLotteryId(UUIDUtils.getUUIDShort());
                    lottery.setFirstNum(firstNum);
                    lottery.setFirstText(arr[(int) firstNum-1]);
                    lottery.setFirstUrl(arrUrl[(int) firstNum-1]);
                    lottery.setSecondNum(secondNum);
                    lottery.setSecondText(arr[(int) secondNum-1]);
                    lottery.setSecondUrl(arrUrl[(int) secondNum-1]);
                    lottery.setThirdNum(thirdNum);
                    lottery.setThirdText(arr[(int) thirdNum-1]);
                    lottery.setThirdUrl(arrUrl[(int) thirdNum-1]);
                    lottery.setSumNum(firstNum+secondNum+thirdNum);
                    lottery.setIssueNo(issueNo+1);
                    lottery.setInputflag("BONUS");
                    //设定开奖时间
                    lottery.setLotteryTime(System.currentTimeMillis());
                    lotteryDao.insert(lottery);
                }else {

                }
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return null;
    }
}
